<template>
  <el-card>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="SPU名称"> {{ spuName || "" }} </el-form-item>

      <el-form-item label="SKU名称">
        <el-input v-model="form.skuName" placeholder="SKU名称"></el-input>
      </el-form-item>

      <el-form-item label="价格(元)">
        <el-input
          type="number"
          v-model="form.price"
          placeholder="价格(元)"
        ></el-input>
      </el-form-item>

      <el-form-item label="重量(千克)">
        <el-input v-model="form.weight" placeholder="重量(千克)"></el-input>
      </el-form-item>

      <el-form-item label="规格描述">
        <el-input
          type="textarea"
          v-model="form.description"
          rows="4"
          placeholder="规格描述 "
        ></el-input>
      </el-form-item>

      <el-form-item label="平台属性">
        <el-form
          ref="attrs"
          label-width="80px"
          v-model="form.attrs"
          :inline="true"
        >
          <el-form-item
            :label="attr.saleAttrName"
            v-for="attr in attrInfoList"
            :key="attr.id"
            width="400px"
          >
            <el-select v-model="attr.attrAndSpu" placeholder="请选择">
              <el-option
                :label="spu.saleAttrValueName"
                :value="`${attr.id}:${spu.id}`"
                v-for="spu in attr.spuSaleAttrValueList"
                :key="spu.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="销售属性">
        <el-form
          ref="attrs"
          label-width="80px"
          v-model="form.attrs"
          :inline="true"
        >
          <el-form-item
            :label="spu.saleAttrName"
            v-for="spu in spuSaleAttrList"
            :key="spu.id"
            width="400px"
          >
            <el-select v-model="spu.spuAndValue" placeholder="请选择">
              <el-option
                :label="sale.saleAttrValueName"
                :value="`${spu.id}:${sale.id}`"
                v-for="sale in spu.spuSaleAttrValueList"
                :key="sale.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="图片列表">
        <!-- @selection-change="handleSelectionChange" -->
        <el-table
          ref="multipleTable"
          tooltip-effect="dark"
          style="width: 100%"
          border
          size="mini"
          :data="spuImageList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center">
          </el-table-column>
          <el-table-column label="图片" width="width">
            <template slot-scope="{ row }">
              <img :src="row.imgUrl" style="width: 100px; height: 120px" />
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width">
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row }">
              <el-button
                type="primary"
                v-if="row.isDefault == 0"
                @click="setIsDafualt(row)"
                >设置默认</el-button
              >
              <el-tag type="success" v-else>默认</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="addSku">保存</el-button>
        <el-button type="" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: "Skuform",
  data() {
    return {
      form: {
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        price: 0,
        skuDefaultImg: "",
        skuDesc: "",
        skuName: "",
        weight: "",
        skuAttrValueList: [],
        skuImageList: [],
        skuSaleAttrValueList: [],
      },
      spuImageList: [],
      spuSaleAttrList: [],
      attrInfoList: [],
      spuName: "",
      dialogTableVisible: false,
    };
  },
  methods: {
    //获取数据
    async getData(category1Id, category2Id, spu) {
      this.spuName = spu.spuName;
      this.form.spuId = spu.id;
      this.form.category3Id = spu.category3Id;
      this.form.tmId = spu.tmId;
      //获取spu图片列表
      let result = await this.$api.default.sku.getSpuImageList(spu.id);
      if (result.code == 200) {
        let imgs = result.data;
        imgs.forEach((i) => (i.isDefault = 0));
        this.spuImageList = imgs;
      } else {
        this.$message({ type: "error", message: "请稍后尝试!" });
        return;
      }
      //获取spu销售属性
      result = await this.$api.default.sku.getSpuSaleAttrList(spu.id);
      if (result.code == 200) {
        this.spuSaleAttrList = result.data;
      } else {
        this.$message({ type: "error", message: "请稍后尝试!" });
        return;
      }
      //获取属性信息列表
      result = await this.$api.default.sku.getAttrInfoList({
        category1Id,
        category2Id,
        category3Id: spu.category3Id,
      });
      result = await this.$api.default.sku.getSpuSaleAttrList(spu.id);
      if (result.code == 200) {
        this.attrInfoList = result.data;
      } else {
        this.$message({ type: "error", message: "请稍后尝试!" });
        return;
      }
    },

    //更改图片是否为默认设置
    setIsDafualt(row) {
      this.spuImageList.forEach((item) => {
        item.isDefault = 0;
        row.isDefault = 1;
      });
    },

    //图片多选项处理
    handleSelectionChange(val) {
      this.form.skuImageList = val;
    },

    //添加
    async addSku() {
      //解析平台属性
      this.form.skuAttrValueList = this.attrInfoList.reduce((pre, current) => {
        if (current.attrAndSpu) {
          current = current.attrAndSpu.split(":");
          pre.push({ attrId: current[0] * 1, valueId: current[1] * 1 });
        }
        return pre;
      }, []);
      //解析销售属性
      this.form.skuSaleAttrValueList = this.spuSaleAttrList.reduce(
        (pre, current) => {
          if (current.spuAndValue) {
            let [saleAttrId, saleAttrValueId] = current.spuAndValue.split(":");
            pre.push({
              saleAttrId: saleAttrId * 1,
              saleAttrValueId: saleAttrValueId * 1,
            });
          }
          return pre;
        },
        []
      );
      //解构图片列表
      this.form.skuImageList = this.form.skuImageList.map((item) => {
        return {
          imgUrl: item.imgUrl,
          imgName: item.imgName,
          isDefault: item.isDefault,
          spuImgId: item.id,
        };
      });

      //添加sku
      let result = await this.$api.default.sku.addSku(this.form);
      if (result == 200) {
        this.$message({
          type: "success",
          message: "操作成功",
        });
        this.$bus.$emit("setNewStatus", 0);
      }
    },

    //取消
    cancel() {
      this.$bus.$emit("setNewStatus", 0);
      this.form = {
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        price: 0,
        skuDefaultImg: "",
        skuDesc: "",
        skuName: "",
        weight: "",
        skuAttrValueList: [],
        skuImageList: [],
        skuSaleAttrValueList: [],
      };
    },
  },
  watch: {
    "form.price"(nval, oval) {
      if (nval < 0) {
        this.form.price = oval * 1;
      }
    },
  },
};
</script>

<style></style>
